package com.amigo.online.provider.search.async;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.amigo.online.provider.search.common.SolrCoreEnum;
import com.amigo.online.provider.search.service.impl.SolrServiceImpl;

@Component
public class AsyncHotWord {
    public static ArrayBlockingQueue<String> queue;
    @Autowired
    private SolrClient client;

    @Async("asyncServiceExecutor")
    public void insertHotWord() {
        try {
            queue = SolrServiceImpl.queue;
            String query = queue.take();
            // 往热词索引库添加业务
            // 为文档添加域
            // 4、为文档添加域
            SolrInputDocument document = new SolrInputDocument();
            document.addField("id", UUID.randomUUID().toString());// 热词ID
            document.addField("hotword", query);// 热词
            client.add(SolrCoreEnum.hotword.toString(), document);
            // 5、向索引库中添加文档。
            client.commit(SolrCoreEnum.hotword.toString());
            Thread.sleep(1000);
        } catch (InterruptedException | SolrServerException | IOException e) {
            e.printStackTrace();
        }

    }
}
